﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class MainPanel : MonoBehaviour
{
    public static MainPanel instance;
    public Button BeginGameButton, AddHurtButton, AddSpeedButton;
    public Button SheZhiButton, ShangChengButton, LiBaoButton, RenWuButton, ChangePlaneButton, TeJiaButton;
    public GameObject can, ZheZhaoImage, player;
    public Text SpeedText, HurtText, HurtSpendText, SpeedSpendText;
    public int Hurt, Speed, Gold, LineNum;
    public int SpeedSpend, HurtSpend;
    public Text WaitOpenText;
    DateTime Today, Lastday;
    public bool IsShow;
    public float UpTipTimes;

    void Awake()
    {
        instance = this;
        can = GameObject.Find("Canvas");
        BeginGameButton.onClick.AddListener(OnclickBeginGameButton);
        AddHurtButton.onClick.AddListener(OnclickAddHurtButton);
        AddSpeedButton.onClick.AddListener(OnclickAddSpeedButton);
        ShangChengButton.onClick.AddListener(OpenShangChengPanel);
        SheZhiButton.onClick.AddListener(OpenSheZhiPanel);
        LiBaoButton.onClick.AddListener(OpenLiBaoPanel);
        RenWuButton.onClick.AddListener(OpenRenWuPanel);
        ChangePlaneButton.onClick.AddListener(OpenChangePanel);
        TeJiaButton.onClick.AddListener(OpenTeJiaPanel);
        player = GameObject.Find("Player");
        GetHurtData(DataManager.Instance.HurtID);
        GetSpeedData(DataManager.Instance.SpeedID);
        initInfo();
        WaitOpenText.gameObject.SetActive(false);
    }

    public void initInfo()
    {
        ZheZhaoImage.GetComponent<Image>().color = new Color(0, 0, 0, 0);
        ZheZhaoImage.GetComponent<Image>().raycastTarget = false;
        ZheZhaoImage.transform.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0);
        ZheZhaoImage.transform.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0);
        ZheZhaoImage.transform.GetComponent<RectTransform>().anchorMin = Vector2.zero;
        ZheZhaoImage.transform.GetComponent<RectTransform>().anchorMax = Vector2.one;
        Debug.Log(" player.transform.localPosition==" + player.transform.localPosition);
        player.GetComponent<Image>().enabled = false;
        Today = DateTime.Now;
        Lastday = DateTime.Parse(DataManager.Instance.Times, System.Globalization.CultureInfo.CurrentCulture); //    字符串带 T、Z：将字符串转为 DateTime 类型，时间是协调世界时间
        Debug.Log("Lastday====" + Lastday);
        RenWu();
        DataManager.Instance.SaveTime();
        TeJiaXianShi();
    }

    /// <summary>
    /// 显示特价
    /// </summary>
    public void TeJiaXianShi()
    {
        Debug.Log("DataManager.Instance.HaveThird==" + DataManager.Instance.HaveThird);
        if (DataManager.Instance.HaveThird == 1)
        {
            TeJiaButton.gameObject.SetActive(false);
        }
        else
        {
            TeJiaButton.gameObject.SetActive(true);
        }
    }

    /// <summary>
    /// 是否进入下一天
    /// </summary>
    public void RenWu()
    {
        if (!IsSameDay(Today, Lastday))
        {
            DataManager.Instance.ClearData();
        }
        else
        {
            Debug.Log("是同一天");
        }
    }

    public bool IsSameDay(DateTime d1, DateTime d2)
    {
        return (d1.Year == d2.Year && d1.Month == d2.Month && d1.Day == d2.Day);
    }

    /// <summary>
    /// /刷新攻击
    /// </summary>
    /// <param name="round"></param>
    public void GetHurtData(int round)
    {
        HurtSpend data = DataManager.Instance.hurtspendcfg.datas[round] as HurtSpend;
        Hurt = data.Lv;
        Debug.Log("Hurt==" + Hurt);
        HurtText.text = (data.id - 10000).ToString() + "bps";
        Debug.Log(" HurtText.text==" + HurtText.text);
        HurtSpend = data.Hurtspend;
        if (HurtSpend >= 1000)
        {
            HurtSpendText.text = ((HurtSpend / 1000) + (float)(HurtSpend % 1000) / 1000).ToString("0.0") + "k";
        }
        else
        {
            HurtSpendText.text = data.Hurtspend.ToString();
        }

        Debug.Log(" HurtSpendText.text==" + HurtSpendText.text);
    }

    /// <summary>
    /// 刷新速度
    /// </summary>
    /// <param name="round"></param>
    public void GetSpeedData(int round)
    {
        SpeedSpend data = DataManager.Instance.speedspendcfg.datas[round] as SpeedSpend;
        LineNum = data.LineNum;
        Speed = data.speed;
        Debug.Log("Speed==" + Speed);
        SpeedText.text = (data.id - 10000).ToString() + "bps";
        Debug.Log(" SpeedText.text==" + SpeedText.text);
        SpeedSpend = data.Speedspend;
        if (SpeedSpend >= 1000)
        {
            SpeedSpendText.text = ((SpeedSpend / 1000) + (float)(SpeedSpend % 1000) / 1000).ToString("0.0") + "k";
        }
        else
        {
            SpeedSpendText.text = data.Speedspend.ToString();
        }

        Debug.Log(" SpeedSpendText.text==" + SpeedSpendText.text);
    }

    /// <summary>
    /// 点击开始按钮
    /// </summary>
    public void OnclickBeginGameButton()
    {
        ShortVoice.instance.PlayVoice("AnNiu");
        DestroyImmediate(gameObject);
        //Destroy(gameObject);
        GameObject obj = Instantiate(Resources.Load<GameObject>("UIPanel/GamePanel"));
        obj.transform.SetParent(can.transform);
        obj.transform.localPosition = Vector3.zero;
        obj.transform.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0);
        obj.transform.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0);
        obj.transform.GetComponent<RectTransform>().anchorMin = Vector2.zero;
        obj.transform.GetComponent<RectTransform>().anchorMax = Vector2.one;
        Player.instance.GetGoldData(DataManager.Instance.Lv);
        DataManager.Instance.PlayTimes += 1;
        DataManager.Instance.Save();
    }

    /// <summary>
    /// /点击增加攻击按钮
    /// </summary>
    public void OnclickAddHurtButton()
    {
        ShortVoice.instance.PlayVoice("AnNiu");
        if (DataManager.Instance.Gold >= HurtSpend)
        {
            //储存金币信息
            DataManager.Instance.Gold -= HurtSpend;
            DataManager.Instance.Save();
            //储存攻击信息
            DataManager.Instance.HurtID += 1;
            DataManager.Instance.Save();
            //更新攻击信息和攻击力
            GetHurtData(DataManager.Instance.HurtID);
            //更新金币显示
            GoldAndLv.instance.GengXin();
            //更新任务进度
            DataManager.Instance.AddHurt += 1;
            DataManager.Instance.Save();
        }
        else
        {
            Debug.Log("金币不足");
        }
    }

    /// <summary>
    /// 点击增加速度按钮
    /// </summary>
    public void OnclickAddSpeedButton()
    {
        ShortVoice.instance.PlayVoice("AnNiu");
        if (DataManager.Instance.Gold >= SpeedSpend)
        {
            //存储金币信息
            DataManager.Instance.Gold -= SpeedSpend;
            DataManager.Instance.Save();
            //存储速度信息
            DataManager.Instance.SpeedID += 1;
            DataManager.Instance.Save();
            //更新速度信息和速度
            GetSpeedData(DataManager.Instance.SpeedID);
            //更新金币显示
            GoldAndLv.instance.GengXin();
            Debug.Log("LineNum=====" + LineNum);
            Player.instance.LineNum = LineNum;
        }
        else
        {
            Debug.Log("金币不足");
        }
    }

    /// <summary>
    /// 打开设置
    /// </summary>
    public void OpenSheZhiPanel()
    {
        GameObject obj = Instantiate(Resources.Load<GameObject>("UIPanel/SheZhiPanel"));
        obj.transform.SetParent(can.transform);
        obj.transform.localPosition = Vector3.zero;
        obj.transform.localPosition = Vector3.zero + new Vector3(0, 100, 0);
        ZheZhaoImage.GetComponent<Image>().color = new Color(0, 0, 0, 0.5f);
        ZheZhaoImage.GetComponent<Image>().raycastTarget = true;
        ShortVoice.instance.PlayVoice("AnNiu");
    }

    /// <summary>
    /// 暂未开启商城
    /// </summary>
    /// <param name="str"></param>
    public void ShowTips(string str)
    {
        WaitOpenText.gameObject.SetActive(true);
        WaitOpenText.text = str;
        IsShow = true;
    }

    /// <summary>
    /// 打开商城
    /// </summary>
    public void OpenShangChengPanel()
    {
        //ShowTips("暂未开启，敬请期待！");
        GameObject obj = Instantiate(Resources.Load<GameObject>("UIPanel/ShangChengPanel"));
        obj.transform.SetParent(can.transform);
        obj.transform.localPosition = Vector3.zero;
        obj.transform.localPosition = Vector3.zero + new Vector3(0, 100, 0);
        ZheZhaoImage.GetComponent<Image>().color = new Color(0, 0, 0, 0.5f);
        ZheZhaoImage.GetComponent<Image>().raycastTarget = true;
        ShortVoice.instance.PlayVoice("AnNiu");
    }

    private void Update()
    {
        if (IsShow)
        {
            UpTipTimes += Time.deltaTime;
            if (UpTipTimes < 0.5f)
            {
                WaitOpenText.transform.Translate(0, 10, 0);
            }
            else
            {
                WaitOpenText.gameObject.SetActive(false);
                WaitOpenText.transform.localPosition = Vector3.zero;
                UpTipTimes = 0;

                IsShow = false;
            }
        }
    }

    /// <summary>
    /// 打开任务
    /// </summary>
    public void OpenRenWuPanel()
    {
        GameObject obj = Instantiate(Resources.Load<GameObject>("UIPanel/RenWuPanel"));
        obj.transform.SetParent(can.transform);
        obj.transform.localPosition = Vector3.zero;
        ZheZhaoImage.GetComponent<Image>().color = new Color(0, 0, 0, 0.5f);
        ZheZhaoImage.GetComponent<Image>().raycastTarget = true;
        ShortVoice.instance.PlayVoice("AnNiu");
    }

    /// <summary>
    /// /打开礼包
    /// </summary>
    public void OpenLiBaoPanel()
    {
        GameObject obj = Instantiate(Resources.Load<GameObject>("UIPanel/LiBaoPanel"));
        obj.transform.SetParent(can.transform);
        obj.transform.localPosition = Vector3.zero;
        ZheZhaoImage.GetComponent<Image>().color = new Color(0, 0, 0, 0.5f);
        ZheZhaoImage.GetComponent<Image>().raycastTarget = true;
        ShortVoice.instance.PlayVoice("AnNiu");
    }

    /// <summary>
    /// 打开飞机切换
    /// </summary>
    public void OpenChangePanel()
    {
        GameObject obj = Instantiate(Resources.Load<GameObject>("UIPanel/ChangePlanePanel"));
        obj.transform.SetParent(can.transform);
        obj.transform.localPosition = Vector3.zero;
        ZheZhaoImage.GetComponent<Image>().color = new Color(0, 0, 0, 0.5f);
        ZheZhaoImage.GetComponent<Image>().raycastTarget = true;
        ShortVoice.instance.PlayVoice("AnNiu");
    }

    /// <summary>
    /// 打开特价
    /// </summary>
    public void OpenTeJiaPanel()
    {
        //ShowTips("暂未开启，敬请期待！");
        GameObject obj = Instantiate(Resources.Load<GameObject>("UIPanel/TeJiaPanel"));
        obj.transform.SetParent(can.transform);
        obj.transform.localPosition = Vector3.zero;
        ZheZhaoImage.GetComponent<Image>().color = new Color(0, 0, 0, 0.5f);
        ZheZhaoImage.GetComponent<Image>().raycastTarget = true;
        ShortVoice.instance.PlayVoice("AnNiu");
    }
}